from .models import UserModel, RoleModel, PowerModel
from rest_framework import serializers, viewsets
from django.views import View

# 序列化器
class UserSearializer(serializers.ModelSerializer):
    roles = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    class Meta:
        model = UserModel
        fields = "__all__"

class RoleSearializer(serializers.ModelSerializer):
    class Meta:
        model = RoleModel
        fields = "__all__"

class PowerSearializer(serializers.ModelSerializer):
    roles = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    class Meta:
        model = PowerModel
        fields = "__all__"


# 视图
class UserViewSet(viewsets.ModelViewSet):
    queryset = UserModel.objects.all()
    serializer_class = UserSearializer

class RoleViewSet(viewsets.ModelViewSet):
    queryset = RoleModel.objects.all()
    serializer_class = RoleSearializer

class PowerViewSet(viewsets.ModelViewSet):
    queryset = PowerModel.objects.all()
    serializer_class = PowerSearializer